home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / sources / fileclasstest.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  3.4 KB  |  105 lines

  1. /*
  2.     File:        FileClassTest.cp
  3.  
  4.     Contains:    TFile is a simple object that does file manipulations    
  5.                   TFileTest.cp contains the TFile class and subclass test functions.
  6.  
  7.     Written by: Kent Sandvik    
  8.  
  9.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #ifndef _FILECLASS_
  25. #include "FileClass.h"
  26. #endif
  27.  
  28. const short kSize = 5;
  29.  
  30. // This is a simple test that will try to create, open, store and retrieve data
  31. // from files.
  32.  
  33. void main(void)
  34. {
  35.     char myString[255]="Test File 1";
  36.     cout << "Start of the TFile object test…\n";
  37.  
  38.     cout << "\nTest x: Create File and get File info (FInfo)…\n";
  39.     TDataFile myFile(myString);
  40.     myFile.Create();
  41.     FInfo myInfo = myFile.GetFileInfo();
  42.     cout << "File signature is " << myInfo.fdCreator << "\n";
  43.  
  44.  
  45.     cout << "\nTest x:Rename and open the file…\n";
  46.     strcpy(myString,"Test File 2");
  47.     myFile.Rename(myString);
  48.     myFile.Open();
  49.  
  50.     cout << "\nTest x: Write and Read handles to file…\n";
  51.     Handle myH = ::NewHandle(kSize);            // handle
  52.     (**myH) = '!';                                // set a value inside the handle
  53.     myFile.WriteHandle(myH);                    // write it down to disk
  54.     Handle result = myFile.ReadHandle();        // read the handle back
  55.     cout << "The character stored  is… " << (**result) << " and it should be ! \n";
  56.     ::DisposeHandle(result);
  57.     ::DisposeHandle(myH);
  58.  
  59.     cout << "\nTest x: Write and Read buffers to file…\n";
  60.     Ptr myBuffer = ::NewPtr(kSize);                // 100 byte buffer
  61.     for (short i = 0; i < kSize; i++)
  62.         myBuffer[i] = 'P';                        // stuff the buffer full of 'P's
  63.     myFile.Reset();                                // set mark to beginning of file
  64.     myFile.Write(myBuffer, kSize);                // write the buffer to disk
  65.  
  66.     Ptr otherBuffer = ::NewPtr(kSize);            // create another buffer
  67.     myFile.Reset();                                // reset to beginning of file
  68.     myFile.Read(otherBuffer, kSize);            // read the bytes    
  69.     for (int i = 0; i < kSize; i++)
  70.         cout << otherBuffer[i];
  71.  
  72.     ::DisposePtr(otherBuffer);
  73.     ::DisposePtr(myBuffer);
  74.  
  75.     cout << "\nTest x: Create a Resource file, and write into it, and read the resource…\n";
  76.     strcpy(myString,"Resource File");
  77.     TResourceFile myResourceFile(myString);
  78.     myResourceFile.Create();
  79.     myResourceFile.Open();
  80.  
  81.     if (myResourceFile.HasResourceFork())
  82.         cout << "We have a resource fork in the resource file created\n";
  83.     else
  84.         cout << "We don't have a resource fork in the resource file created.\n";
  85.  
  86.     myResourceFile.Update();
  87.     myResourceFile.Assign();
  88.  
  89.     cout << "\nTest x:Close and delete the files…\n";
  90.     myFile.Close();
  91.     myResourceFile.Close();
  92.     myFile.Delete();
  93.     myResourceFile.Delete();
  94.  
  95.     cout << "\nEnd of the TFile object test!\n";
  96. }
  97.  
  98.  
  99. // _________________________________________________________________________________________________________ //
  100. /*    Change History (most recent last):
  101.   No        Init.    Date        Comment
  102.   1            khs        12/27/92    New file
  103.   2            khs        1/14/93        Cleanup
  104. */
  105.